package com.ming.luckyDemo.controller;

import com.ming.luckyDemo.pojo.DO.BetBillDO;
import com.ming.luckyDemo.pojo.DO.UserInfoDO;
import com.ming.luckyDemo.pojo.entity.ResultBody;
import com.ming.luckyDemo.service.SelectHistoryBetSercvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@RestController
public class SelectHistoryBetController {

    @Autowired
    private SelectHistoryBetSercvice selectHistoryBetSercvice;

    @PostMapping("/selectBet")
    public ResultBody selectBet(HttpServletRequest request,HttpServletResponse response){
//        response.setHeader("Access-Control-Allow-Credentials","true");
        Cookie[] cookies = request.getCookies();
        response.setHeader("Set-Cookie", response.getHeader("Set-Cookie")+";SameSite=None");
        if (Objects.isNull(cookies)){
            return ResultBody.error().message("未登录");
        }
        for (Cookie cookie : cookies) {
            UserInfoDO cookie1 = selectHistoryBetSercvice.getCookie(cookie.getName());
            if (cookie.getName().equals(cookie1.getUsername()) && cookie.getValue().equals(cookie1.getSessionKey())) {
                List<BetBillDO> betBillDOS = selectHistoryBetSercvice.selectBet(cookie.getName());
                return ResultBody.ok().message("下注单").data("bet",betBillDOS);
            }
        }
        return ResultBody.error().message("cookie错误");
    }

}
